@tool
class_name ContextBehavior_LookAtVelDirection
extends BaseContextBehavior

## FIX：从0°转到180°，可能出现顺时针和逆时针两种况

#region 属性
@export var force:float
## 小于等于这个值认为旋转对齐
@export var alignment_tolerance:float
## 减速角度
@export var deceleration_rotation:float
## 最小转向速度
@export var min_vel_length:float = 0.1
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
func init():
	super()
func calc_steering(behaviors_owner:Node,agent:SteeringBehaviorAgent,delta:float):
	super(behaviors_owner,agent,delta)
	var to_target:Vector2 = agent.get_vel()
	if agent.linear_velocity.length() < min_vel_length:
		return
	var target_rot = to_target.normalized().angle()
	var angular_interest:float = get_match_rotation(target_rot) * sensitive
	agent.add_angular_forcce_interest(angular_interest * force)
#endregion
#region 公共方法
func get_match_rotation(target_rot:float) -> float:
	var rotation = wrapf(target_rot - agent.get_rot(),-PI,PI)
	var rotation_abs = absf(rotation)
	if rotation_abs <= deg_to_rad(alignment_tolerance):
		return 0
	var desired_rotation = agent.angular_speed_max
	if rotation_abs <= deg_to_rad(deceleration_rotation):
		desired_rotation *= rotation_abs / deg_to_rad(deceleration_rotation)
	desired_rotation *= rotation / rotation_abs
	return desired_rotation
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _init() -> void:
	if Engine.is_editor_hint():
		resource_local_to_scene = true
	behavior_type = GameEnum.ContextBehaviorType.LookAtVelDirection
func draw() -> void:
	super.draw()
#endregion
